package demo.demo10;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//性能分析
public class test10 {
    //普通方法
    public static void test01() {
        User01 user01 = new User01();
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            user01.getName();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("普通方法：" + (endTime - startTime) + "ms");
    }

    //反射方法调用
    public static void test02() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        User01 user01 = new User01();
        Class aClass = user01.getClass();

        Method getName = aClass.getDeclaredMethod("getName", null);

        long startTime = System.currentTimeMillis();

        for (int i = 0; i < 1000000000; i++) {
            getName.invoke(user01, null);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("反射：" + (endTime - startTime) + "ms");
    }

    //关闭检测
    public static void test03() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        User01 user01 = new User01();
        Class aClass = user01.getClass();

        Method getName = aClass.getDeclaredMethod("getName", null);
        getName.setAccessible(true);
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < 1000000000; i++) {
            getName.invoke(user01, null);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("反射：" + (endTime - startTime) + "ms");
    }

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        test01();
        test02();
        test03();
    }
}
